/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doghost.reports;

import doghost.DoghostApp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JFrame;

/**
 *
 * @author duo
 */
public class ContasReceberPeriodo {

    public ContasReceberPeriodo() throws java.text.ParseException {
        executarContasReceberPeriodo(new Boolean(false));
    }

    public ContasReceberPeriodo(Boolean recebidas) throws java.text.ParseException {
        executarContasReceberPeriodo(recebidas);
    }

    public void executarContasReceberPeriodo(Boolean recebidas) throws java.text.ParseException {
        ReportRunner reportRunner = new ReportRunner("doghostPU");
        reportRunner.setReportFileName("/doghost/reports/ContasReceberPeriodo.jasper");
        HashMap parameters = new HashMap();
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        ContasPorPeriodoParametersSelection contasPorPeriodoParametersSelection =
                new ContasPorPeriodoParametersSelection(mainFrame, true);
        contasPorPeriodoParametersSelection.setLocationRelativeTo(mainFrame);
        contasPorPeriodoParametersSelection.setVisible(true);
        if (!contasPorPeriodoParametersSelection.isSaveFired()) {
            return;
        }
        Date dataInicial = contasPorPeriodoParametersSelection.getDataInicial();
        Date dataFinal = contasPorPeriodoParametersSelection.getDataFinal();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(dataFinal);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        dataFinal = calendar.getTime();
        parameters.put("RECEBIDAS", recebidas);
        parameters.put("DATA_INICIO", dataInicial);
        parameters.put("DATA_FIM", dataFinal);
        reportRunner.setParametersMap(parameters);
        if (recebidas) {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                    "doghostPU", new HashMap());
            EntityManager em = emf.createEntityManager();
            Query query = em.createQuery("SELECT h FROM Hospedagem h WHERE h.pagamento IS NOT NULL");
            reportRunner.setReportQuery(query);
        }
        reportRunner.runReport();
    }
}
